//
//  CZNoticeViewController.m
//  nsj_iOS
//
//  Created by apple on 2017/1/12.
//  Copyright © 2017年 GCZ. All rights reserved.
//

#import "CZNoticeViewController.h"
#import "CZNewsCenterViewController.h"
#import "CZGoodWorkViewController.h"
#import "CZPlatformAnnouncementViewController.h"
#import "CZCartoonViewController.h"

@interface CZNoticeViewController () <UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *newsCenterBtn;
@property (weak, nonatomic) IBOutlet UIButton *platformNoticeBtn;
@property (weak, nonatomic) IBOutlet UIButton *goodWorksBtn;
@property (weak, nonatomic) IBOutlet UIButton *cartoonSerialBtn;

@property (weak, nonatomic) IBOutlet UILabel *badgeLabel;
@property (weak, nonatomic) IBOutlet UIView *containerView;

@property (nonatomic, strong) CZNewsCenterViewController *newsVC;
@property (nonatomic, strong) CZGoodWorkViewController *goodWorkVC;
@property (nonatomic, strong) CZPlatformAnnouncementViewController *noticeVC;
@property (nonatomic, strong) CZCartoonViewController *cartoonVC;


@property (nonatomic, copy) NSArray *btnArray;

@end

@implementation CZNoticeViewController
- (NSArray *__nonnull)btnArray {
    if (!_btnArray) {
        _btnArray = @[_newsCenterBtn, _platformNoticeBtn, _goodWorksBtn, _cartoonSerialBtn];
    }
    return _btnArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"公告中心";
    self.navigationController.delegate = self;

    [(UIView*)[self.navigationController.navigationBar.subviews objectAtIndex:0] setAlpha:1.f];
    
    
//    _newsCenterBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    for (UIButton *button in self.btnArray) {
        button.imageView.contentMode = UIViewContentModeScaleAspectFit;        
    }
    _cartoonSerialBtn.titleEdgeInsets = UIEdgeInsetsMake(50, -50, 15, 0);
    _platformNoticeBtn.titleEdgeInsets = UIEdgeInsetsMake(50, -40, 15, 0);

    
    self.badgeLabel.layer.cornerRadius = self.badgeLabel.width / 2;
    self.badgeLabel.clipsToBounds = YES;
    
    _newsVC = [[CZNewsCenterViewController alloc] init];
    _goodWorkVC = [[CZGoodWorkViewController alloc] init];
    _noticeVC = [[CZPlatformAnnouncementViewController alloc] init];
    _cartoonVC = [[CZCartoonViewController alloc] init];
    
    [self addChildViewController:_newsVC];
    [self addChildViewController:_goodWorkVC];
    [self addChildViewController:_noticeVC];
    [self addChildViewController:_cartoonVC];
    
    _newsVC.view.frame = self.containerView.bounds;
    _newsVC.newsTableView.bounds = self.containerView.bounds;
    [self.containerView addSubview:_newsVC.view];
    
    [_newsVC postNewsData];
    _newsCenterBtn.selected = YES;
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (IBAction)newsAction:(UIButton *)sender {
    
    for (UIButton *button in self.btnArray) {
        if (button != _newsCenterBtn) {
            button.selected = NO;
        } else {
            button.selected = YES;
        }
    }
    
    [self.containerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    _newsVC.view.frame = self.containerView.bounds;
    [self.containerView addSubview:_newsVC.view];
}
- (IBAction)noticeAction:(UIButton *)sender {
    for (UIButton *button in self.btnArray) {
        if (button != _platformNoticeBtn) {
            button.selected = NO;
        } else {
            button.selected = YES;
        }
    }
    [self.containerView.subviews[0] removeFromSuperview];
    _noticeVC.view.frame = self.containerView.bounds;
    [self.containerView addSubview:_noticeVC.view];
    
}
- (IBAction)goodWorkAction:(UIButton *)sender {
    for (UIButton *button in self.btnArray) {
        if (button != _goodWorksBtn) {
            button.selected = NO;
        } else {
            button.selected = YES;
        }
    }
    [self.containerView.subviews[0] removeFromSuperview];
    _goodWorkVC.view.frame = self.containerView.bounds;
    [self.containerView addSubview:_goodWorkVC.view];
}
- (IBAction)cartoonAction:(UIButton *)sender {
    for (UIButton *button in self.btnArray) {
        if (button != _cartoonSerialBtn) {
            button.selected = NO;
        } else {
            button.selected = YES;
        }
    }
    [self.containerView.subviews[0] removeFromSuperview];
    _cartoonVC.view.frame = self.containerView.bounds;
    [self.containerView addSubview:_cartoonVC.view];
    [_cartoonVC postCartoonData];
}

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
